package com.ag.zhh.service;

import java.util.Date;
import java.util.List;

import com.ag.bean.TbEvent;
import com.ag.bean.TbEventGrade;
import com.ag.bean.TbEventLevel;
import com.ag.bean.TbUser;
import com.ag.dao.TbEventGradeDAO;
import com.ag.dao.TbEventLevelDAO;
import com.ag.wzp.dao.WzpTbEventDAO;
import com.ag.wzp.service.impl.EventBaseService;
import com.ag.zhh.dao.ZHHEventGradeDAO;

public class EventUpGradeService {
	/**
	 * 查找所有已审核的事件等级记录
	 * @param eventId
	 * @return
	 */
	public List<TbEventGrade> findAuditedEventUpGradeByEId(String eventId) {
		List<TbEventGrade> eventGradeList = null;
		eventGradeList = eventGradeDAO.findAuditedByPropertyOrderByTime("tbEvent.EId", eventId);
		return eventGradeList;
	}
	/**
	 * 查找未审核的事件等级更改记录
	 * @param eventId
	 * @return
	 */
	public TbEventGrade findApplyedEventUpGradeByEId(String eventId) {
		TbEventGrade eventGrade = null;
		List<TbEventGrade> list = eventGradeDAO.findApplyedByPropertyOrderByTime("tbEvent.EId", eventId);
		if(list != null && list.size() > 0){
			eventGrade = list.get(0);
		}
		return eventGrade;
	}
	
	/**
	 * 申请事件等级变更
	 * @param EId
	 * @return
	 */
	public TbEventGrade eventUpGrade(String EId,String newLevelId,TbUser applyUser,String applyContent) {
		TbEvent event = eventDAO.findById(EId);
		
		//新的事件等级变更
		TbEventGrade eventGrade = new TbEventGrade();
		eventGrade.setEgOgrade(event.getTbEventLevel().getElId());
		eventGrade.setEgNgrade(newLevelId);
		eventGrade.setTbEvent(event);
		eventGrade.setTbUserByEgRequser(applyUser);
		eventGrade.setEgReqResion(applyContent);
		eventGrade.setEgReqTime(new Date());
		eventGradeDAO.save(eventGrade);
		return eventGrade;
	}
	
	/**
	 * 审核事件等级变更
	 * @param egId
	 * @param auditUser
	 * @param auditContent
	 * @param egAgree
	 * @return
	 */
	public TbEventGrade eventUpGradeAudit(String egId,TbUser auditUser,String auditContent,int egAgree) {
		TbEventGrade eventGrade = eventGradeDAO.findById(egId);
		eventGrade.setEgAudResion(auditContent);
		eventGrade.setEgAudTime(new Date());
		eventGrade.setTbUserByRgAudit(auditUser);
		if(egAgree == 1) {
			//eventGrade.setEgAgree(new Long(1));
			eventGrade.setEgAgree(new Integer(1));
			TbEvent event = eventDAO.findById(eventGrade.getTbEvent().getEId());
			TbEventLevel newEventLevel = eventLevelDAO.findById(eventGrade.getEgNgrade());
			event.setTbEventLevel(newEventLevel);
			eventDAO.attachDirty(event);
		}else {
			//eventGrade.setEgAgree(new Long(0));
			eventGrade.setEgAgree(new Integer(0));
		}
		eventGradeDAO.attachDirty(eventGrade);
		return eventGrade;
	}
	
	private ZHHEventGradeDAO eventGradeDAO;
	
	private WzpTbEventDAO eventDAO;
	
	private EventBaseService eventBaseService;
	
	private TbEventLevelDAO eventLevelDAO;

	public WzpTbEventDAO getEventDAO() {
		return eventDAO;
	}

	public void setEventDAO(WzpTbEventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}

	public EventUpGradeService() {
		super();
	}

	
	public ZHHEventGradeDAO getEventGradeDAO() {
		return eventGradeDAO;
	}

	public void setEventGradeDAO(ZHHEventGradeDAO eventGradeDAO) {
		this.eventGradeDAO = eventGradeDAO;
	}

	public EventBaseService getEventBaseService() {
		return eventBaseService;
	}

	public void setEventBaseService(EventBaseService eventBaseService) {
		this.eventBaseService = eventBaseService;
	}

	public TbEventLevelDAO getEventLevelDAO() {
		return eventLevelDAO;
	}

	public void setEventLevelDAO(TbEventLevelDAO eventLevelDAO) {
		this.eventLevelDAO = eventLevelDAO;
	}
	
	
}	
